// pages/interface/index.js
Page({
  data: {
    books: [],
    keywords: '',
    history: wx.getStorageSync('history') || []
  },
  onLoad() {
    this.getBooks()
  },
  //调接口的方法
  getBooks() {
    //显示loaging效果
    wx.showLoading({
      title: '正在加载...',
      mask: true
    })
    // 调用小程序的 API 发起请求
    wx.request({
      //地址接口
      url: 'https://hmajax.itheima.net/api/books',
      //请求方法
      method: 'GET',
      //请求时提交的数据
      data: {
        creator: 'zhangsan'
      },
      //成功的回调
      success: (request) => {
        //更新数据渲染页面
        this.setData({
          books: request.data.data
          //成功提示

        })
        wx.showToast({
          title: '获取书单成功！',
          mask: true,
          duration: 3000,


        })
      },
      fail() {
        //失败回调
      },
      complete() {
        wx.hideLoading()
      }
    })
  },
  //存
  setStorage() {
    wx.setStorageSync('sfff', '大帅逼')
    wx.setStorageSync('zmmm', {
      name: 'zm',
      age: 18
    })
  },
  //取
  getStorage() {
    const sfff = wx.getStorageSync('sfff')
    console.log(sfff);
    const zmmm = wx.getStorageSync('zmmm')
    console.log(zmmm);
  },
  //删
  removeStorage() {
    wx.removeStorageSync('logs')
  },
  //清空|
  clearStorage() {
    wx.clearStorageSync()
  },
  search() {
    const {
      keywords,
      history
    } = this.data
    history.push(keywords)
    //更新数据重新渲染
    this.setData({
      history,
      keywords: ''
    })
    //记录数据到本地
    wx.setStorageSync('history', history)
  }

})